<?
include "../db_config.php";
function XML2Array ( $xml ) 
{ 
    $array = simplexml_load_string ( $xml ); 
    $newArray = array ( ) ; 
    $array = ( array ) $array ; 
    foreach ( $array as $key => $value ) 
    { 
        $value = ( array ) $value ; 
        $newArray [ $key] = $value [ 0 ] ; 
    } 
    $newArray = array_map("trim", $newArray); 
  return $newArray ; 
} 
function make_insert_sql($tableName, $item)
{
    $sql = "insert into $tableName (";
    $del = "";
    foreach($item as $key => $val)
    {
		if($val == null || $val == "")
			continue;
        $sql = $sql . $del .$key;
        if($del == "")
            $del = ", ";
    }
    $sql .= ") values(";
    $del = "";
    foreach($item as $key => $val)
    {
		if($val == null || $val == "")
			continue;
        $sql = $sql . $del ."'".$val."'";
        if($del == "")
            $del = ", ";
    }
    $sql .=");";

    return $sql;
}
function make_update_sql($tableName, $item, $condition)
{
	$sql = "update $tableName set ";
	$del = "";
	foreach($item as $key => $val)
	{
		if($val == null || $val == "")
			continue;
		$sql = $sql . $del .$key . "='". $val."'";
		if($del == "")
			$del = ", ";
	}
	$sql .= " where $condition";
	return $sql;
}
function debug_print($str)
{
	//echo "$str<BR>\n";
}

function unixToMySQL($timestamp)
{
	$timestamp = intval($timestamp);
	//echo "date : $timestamp";
	//$form = "date(''Y-m-d H:i:s', $timestamp);";
	//debug_print($form);

	//echo "after : $timestamp";
	return date('Y-m-d H:i:s', $timestamp);
}
//test sample xml
/*
$xml_str = 
"<?xml version=\"1.0\"?><document>
<submitId>xlos</submitId><submitDeviceMac>12:32:3d:24:0d:8f</submitDeviceMac><item><latitude>128.123456</latitude><longitude>38.7654321</longitude><bssid>00:11:3d:24:0d:8f</bssid><ssid>Home</ssid><signal>80</signal><noise>30</noise><snr>70</snr><type>BBS</type><channel>5</channel><bcnInterval>100</bcnInterval><qos>2</qos><velocity>40</velocity><gpsStrength></gpsStrength><detectTime>1252848668</detectTime></item><item><latitude>128.123456</latitude><longitude>39.7654321</longitude><bssid>11:12:3d:24:0d:8f</bssid><ssid>Office</ssid><signal>60</signal><noise>20</noise><snr>50</snr><type>BBS</type><channel>5</channel><bcnInterval>100</bcnInterval><qos>2</qos><velocity>60</velocity><gpsStrength></gpsStrength><detectTime>1252848700</detectTime></item></document>";
*/

/*
$xml_str =
"<?xml version=\"1.0\"?><document>
<submitId>uzys</submitId>
<submitDeviceMac>00:23:45:3C:B4:7D
</submitDeviceMac>
<item>
<latitude>47.6336283333333</latitude>
<longitude>-122.18646</longitude>
<velocity>0.111175</velocity>
<hdop>1.4</hdop>
<detectTime>1099077644</detectTime>
<ap>
<bssid>00:1E:58:33:E3:13</bssid>
<ssid>dlink</ssid>
<signal>-74</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>3</channel>
</ap>
<ap>
<bssid>00:23:04:88:EF:25</bssid>
<ssid>SCGuest</ssid>
<signal>-74</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>1</channel>
</ap>
<ap>
<bssid>00:23:04:88:EE:70</bssid>
<ssid></ssid>
<signal>-79</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:23:04:88:EF:20</bssid>
<ssid></ssid>
<signal>-78</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>1</channel>
</ap>
<ap>
<bssid>00:1C:DF:DA:F5:47</bssid>
<ssid>hwp</ssid>
<signal>-77</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:23:04:88:E9:D5</bssid>
<ssid>SCGuest</ssid>
<signal>-85</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>11</channel>
</ap>
<ap>
<bssid>00:23:04:88:DF:52</bssid>
<ssid></ssid>
<signal>-89</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:08:9F:C2:0B:8A</bssid>
<ssid>iptime</ssid>
<signal>-66</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:08:9F:B0:2C:3C</bssid>
<ssid>RK4_Embedded</ssid>
<signal>-84</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:08:9F:C2:0D:9E</bssid>
<ssid>602</ssid>
<signal>-83</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>13</channel>
</ap>
<ap>
<bssid>00:23:04:88:EE:75</bssid>
<ssid>SCGuest</ssid>
<signal>-83</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:23:04:88:E4:65</bssid>
<ssid>SCGuest</ssid>
<signal>-83</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:23:04:88:EF:22</bssid>
<ssid></ssid>
<signal>-75</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>1</channel>
</ap>
<ap>
<bssid>00:23:04:88:EE:72</bssid>
<ssid></ssid>
<signal>-79</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:23:04:88:F0:B2</bssid>
<ssid>LGEWEP</ssid>
<signal>-83</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>11</channel>
</ap>
<ap>
<bssid>00:23:04:88:DF:50</bssid>
<ssid></ssid>
<signal>-91</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:23:04:88:F0:B0</bssid>
<ssid></ssid>
<signal>-86</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>11</channel>
</ap>
<ap>
<bssid>00:23:04:88:E9:D2</bssid>
<ssid>LGEWEP</ssid>
<signal>-85</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>11</channel>
</ap>
<ap>
<bssid>00:23:04:88:E4:62</bssid>
<ssid>LGEWEP</ssid>
<signal>-76</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:1F:33:FC:B1:F5</bssid>
<ssid>dlnatest2</ssid>
<signal>-85</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>11</channel>
</ap>
<ap>
<bssid>00:23:04:88:F0:B5</bssid>
<ssid>SCGuest</ssid>
<signal>-86</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>11</channel>
</ap>
</item>
<item>
<latitude>47.6336283333333</latitude>
<longitude>-122.18646</longitude>
<velocity>0.146456</velocity>
<hdop>1.4</hdop>
<detectTime>1099077645</detectTime>
<ap>
<bssid>00:1E:58:33:E3:13</bssid>
<ssid>dlink</ssid>
<signal>-74</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>3</channel>
</ap>
<ap>
<bssid>00:23:04:88:EF:25</bssid>
<ssid>SCGuest</ssid>
<signal>-74</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>1</channel>
</ap>
<ap>
<bssid>00:23:04:88:EE:70</bssid>
<ssid></ssid>
<signal>-79</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:23:04:88:EF:20</bssid>
<ssid></ssid>
<signal>-78</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>1</channel>
</ap>
<ap>
<bssid>00:1C:DF:DA:F5:47</bssid>
<ssid>hwp</ssid>
<signal>-77</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:23:04:88:E9:D5</bssid>
<ssid>SCGuest</ssid>
<signal>-85</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>11</channel>
</ap>
<ap>
<bssid>00:23:04:88:DF:52</bssid>
<ssid></ssid>
<signal>-89</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:08:9F:C2:0B:8A</bssid>
<ssid>iptime</ssid>
<signal>-66</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:08:9F:B0:2C:3C</bssid>
<ssid>RK4_Embedded</ssid>
<signal>-84</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:08:9F:C2:0D:9E</bssid>
<ssid>602</ssid>
<signal>-83</signal>
<type>BSS</type>
<encryption>0</encryption>
<channel>13</channel>
</ap>
<ap>
<bssid>00:23:04:88:EE:75</bssid>
<ssid>SCGuest</ssid>
<signal>-83</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
<ap>
<bssid>00:23:04:88:E4:65</bssid>
<ssid>SCGuest</ssid>
<signal>-83</signal>
<type>BSS</type>
<encryption>1</encryption>
<channel>6</channel>
</ap>
</item>
</document>";
*/

//for backward compatibility
if($HTTP_RAW_POST_DATA == null)
	$HTTP_RAW_POST_DATA = $HTTP_POST_VARS[xml];

// Remove unwanted slashes
if(get_magic_quotes_gpc())
	$xml_str = stripslashes($HTTP_RAW_POST_DATA);
else
	$xml_str = $HTTP_RAW_POST_DATA;

if($xml_str == null || $xml_str == "")
{
	echo "HTTP POST ERROR\n";
	exit(0);
}


//insert post data to DB
$post_data = array();
$post_data[post_data] = $xml_str;
$post_data[size] = strlen($xml_str);
$sql = make_insert_sql("wps_submit_data", $post_data);
mysql_query($sql);


//pasrse xml data
$xml = simplexml_load_string($xml_str);
$str = array();
$submitId = strval($xml->submitId);
$submitDeviceMac = strtolower($xml->submitDeviceMac);
$no = sizeof($xml->item);
$current_time = unixToMySQL(time());

echo "HTTP POST OK\n";

$appended_ap_no = 0;
$appended_log_no = 0;
$update_ap_pos = 0;

foreach($xml->item as $gps)
{
	$gps_data = array();
	$gps_data[latitude] = doubleVal($gps->latitude);
	$gps_data[longitude] = doubleVal($gps->longitude);
	$gps_data[velocity] = doubleVal($gps->velocity);
	$gps_data[hdop] = doubleVal($gps->hdop);
	$gps_data[detectTime] = unixToMySQL($gps->detectTime);
	$gps_data[pos] = $gps->latitude . " " . $gps->longitude;

	foreach($gps->ap as $ap)
	{
		$item = array();
		$item = $gps_data;
		$item[bssid] = $ap->bssid;
		$sql = "select * from wps_ap_info where bssid like '".$item[bssid]."'";
		$result = mysql_query($sql);
		$data = mysql_fetch_array($result);

		$item[ssid] = $ap->ssid;
		$item[type] = $ap->type;
		//$item[lastSeen] = $current_time;
		$item[lastSeen] = $gps_data[detectTime];
		$item[encryption] = $ap->encryption;
		$item[speed] = $ap->speed;

		//this attributes are not appended to wps_gps_info table
		$item[hdop] = NULL;;
		$item[velocity] = NULL;
		$item[detectTime] = NULL;
		
		//$item[channel] = $ap->channel;
		//$item[bcnInterval] = $ap->bcnInterval;
		//$item[qos] = $ap->qos;
		
		//if ap information is not in DB
		if($data == NULL)
		{
			debug_print("insert to wps_ap_info");
			//$item[firstSeen] = $current_time;
			$item[firstSeen] = $gps_data[detectTime];
			$item[highestSignal] = $ap->signal;
			$item[highestSNR] = $ap->snr;
			$item[sourceNum] = 1;
			$sql = make_insert_sql("wps_ap_info", $item, $item);
			debug_print($sql);
			//echo $sql;
			//append this ap's information to wps_ap_info table
			mysql_query($sql);

			//find ap id in DB
			$sql = "select * from wps_ap_info where bssid like '".$item[bssid]."'";
			debug_print($sql);
			$result = mysql_query($sql);
			$data = mysql_fetch_array($result);
			$appended_ap_no++;
			echo "$appended_ap_no. new ap (".$ap->bssid.") is appended<BR>\n";
		}
		//if ap is already in DB
		else
		{
			//get total log of this specific ap
			$sql = "select COUNT(*) from wps_ap_detection_log where id='".$data[id]."'";
			$r = mysql_query($sql);
			$d = mysql_fetch_array($r);
			//this code needs to be changed
			//if the detect log is duplicated, the log is not inserted in DB but this count always
			//increases
			$item[sourceNum] = intval($d['COUNT(*)']) + 1;

			//update information
			debug_print("update to wps_ap_info");
			if($ap->signal > $data[highestSignal])
				$item[highestSignal] = $ap->signal;
			if($ap->snr > $data[highestSNR])
				$item[highestSNR] = $ap->snr;
			$sql = make_update_sql("wps_ap_info", $item, "bssid = '".$item[bssid]."'");
			mysql_query($sql);
			debug_print($sql);
		}
			
		debug_print("insert to wps_ap_detection_log");

		//reset array
		$item = array();
		$item = $gps_data;
		$item[id] = $data[id];
		$item[submitTime] = $current_time; 
		$item[submitId] = $submitId;
		$item[submitDeviceMac] = $submitDeviceMac;
		$item[signal] = $ap->signal;
		$item[noise] = $ap->noise;
		$item[snr] = $ap->snr;

		//ignore this attritube in wps_ap_detection_log
		$item[pos] = NULL;
		$sql = make_insert_sql("wps_ap_detection_log", $item);

		//add to detection log
		debug_print($sql);
		mysql_query($sql);
		$appended_log_no++;
		echo "$appended_log_no. new log (".$item[latitude].", ".$item[longitude].") is appended<BR>\n";

		//update ap position
		//refine the algorithm later
		//currently, highest signal value is used
		$item = array();
		if($ap->signal >= $data[highestSignal])
		{
			debug_print("update ap position");
			$item[id] = $data[id];
			$item[latitude] = $gps_data[latitude];
			$item[longitude] = $gps_data[longitude];
			$item[pos] = $gps_data[pos];
			$sql = make_update_sql("wps_ap_info", $item, "bssid = '".$ap->bssid."'");
			mysql_query($sql);
		}
	}
}

echo "<BR>\nTotal appended AP : $appended_ap_no<BR>\n";
echo "Total appended Log : $appended_log_no<BR>\n";
?>
